package org.qydjk.console.controller.basic;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.ConfigCategory;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/configCategory")
public class ConfigCategoryController extends BaseController{

    /**
     *系统分类配置查询
     * @return
     */
    @RequestMapping("/dataGrid.do")
    @ResponseBody
    public Object dataGrid(Integer page, Integer rows, String sort, String order,String statusType,String configCategoryStatus) {
        String hql ="select * from qy_config_category";
        String totalSql ="select count(id) from qy_config_category";
        if (!StringUtil.isEmptyOrNull(statusType)) {
            hql = hql+" WHERE name LIKE '%" + statusType + "%'";
            totalSql = totalSql+" WHERE name LIKE '%" + statusType + "%'";
        }
        if(!StringUtil.isEmptyOrNull(configCategoryStatus)){
            hql = hql+" WHERE status = " + configCategoryStatus + " ";
            totalSql = totalSql+" WHERE status = " + configCategoryStatus + "";
        }
        List<ConfigCategory> configCategories = new ArrayList<ConfigCategory>();
        try {
            configCategories = baseService.findBySQL(hql, ConfigCategory.class, new HibernatePage(page, rows));
            int total = baseService.getRecordCountSQL(totalSql);
            return new EasyUIResponse(total, configCategories);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse(0, configCategories);

    }

    /**
     * 添加页
     * @return
     */
    @RequestMapping("/addPage.do")
    public String configCategoryAdd(){
        return "basic/configCategory/configCategoryAdd";
    }

    /**
     * 添加
     * @param configCategory
     * @return
     * @throws Exception
     */
    @PostMapping("/add.do")
    @ResponseBody
    public Object add(@Valid ConfigCategory configCategory) throws Exception {
        Date date = new Date();
        configCategory.setCreateTime(date);
        baseService.save(configCategory);
        return renderSuccess("添加成功！");
    }

    /**
     * 编辑页
     * @return
     */
    @RequestMapping("/editPage.do")
    public String configCategorEdit(Model model, Integer id){
        try {
            ConfigCategory configCategory = baseService.get(ConfigCategory.class,id);
            model.addAttribute("ConfigCategory", configCategory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "basic/configCategory/configCategoryEdit";
    }

    /**
     * 编辑
     * @param configCategory
     * @return
     * @throws Exception
     */
    @RequestMapping("/edit.do")
    @ResponseBody
    public Object edit(@Valid ConfigCategory configCategory) throws Exception {
        Date date = new Date();
        baseService.update(configCategory);
        configCategory.setUpdateTime(date);
        return renderSuccess("编辑成功！");
    }
    /**
     * 删除配置
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public Object delete(Integer id) throws Exception {
        ConfigCategory configCategory = new ConfigCategory();
        configCategory.setId(id);
        baseService.delete(configCategory);
        return renderSuccess("删除成功！");

    }
}
